package com.shujia.interfacer;

import com.shujia.reflect.DataRead;
import com.shujia.reflect.Score;
import com.shujia.reflect.Student;

import java.util.ArrayList;
import java.util.Comparator;

public class Demo4Score {

    public static void main(String[] args) {

        ArrayList<Score> scores = DataRead.read("java/data/score.txt", Score.class);

        DataSet<Score> dataSet = new DataSet<>(scores);


        dataSet.reduce(new KeySelect<Score, String>() {
            @Override
            public String select(Score score) {
                return score.getSid();
            }
        }, new ValueSelect<Score>() {
            @Override
            public Long select(Score score) {
                return score.getScore().longValue();
            }
        }).sort(new Comparator<KeyValue>() {
            @Override
            public int compare(KeyValue o1, KeyValue o2) {
                return (int) ((Long) o2.getValue() - (Long) o1.getValue());
            }
        }).take(10).print();


        System.out.println("============");
        ArrayList<Student> students = DataRead.read("java/data/students.txt", Student.class);


        DataSet<Student> set = new DataSet<>(students);

        //统计班级的人数
        set.map(new Funcation<Student, String>() {
            @Override
            public String map(Student i) {
                return i.getClazz() + "," + 1;
            }
        }).reduce(new KeySelect<String, String>() {
            @Override
            public String select(String kv) {
                return kv.split(",")[0];
            }
        }, new ValueSelect<String>() {
            @Override
            public Long select(String kv) {
                return Long.parseLong(kv.split(",")[1]);
            }
        }).take(2).print();


    }
}
